package com.neu.his.service.registFee02.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neu.his.bo.fee.FeeRec;
import com.neu.his.bo.removeDiag.RemoveItem;
import com.neu.his.bo.removeDiag.RemoveRec;
import com.neu.his.bo.removeDiag.ReomveQSend;
import com.neu.his.core.Result;
import com.neu.his.entity.Patient;
import com.neu.his.entity.Regist;
import com.neu.his.mapper.*;
import com.neu.his.service.registFee02.RemoveDiagService;
import com.neu.his.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Service
public class RemoveDiagImpl implements RemoveDiagService {
    @Autowired
    private RegistMapper registMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RegistLevelMapper registLevelMapper;

    @Override
    public Result getItem(FeeRec feeRec){
        QueryWrapper<Patient> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(Patient::getIdNumber,feeRec.getPatientId());
        List<Patient> patientList = patientMapper.selectList(queryWrapper1);
        if(patientList.size() == 0){
            return Result.fail("身份证号错误");
        }
        List<RemoveItem> list = new ArrayList<>();
        List<Regist> registList = new ArrayList<>();
        QueryWrapper<Regist> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Regist::getPatientId,patientList.get(0).getId());
        registList = registMapper.selectList(queryWrapper);
        for(Regist regist:registList){
            RemoveItem removeItem = new RemoveItem();
            removeItem.setRegistId(regist.getId());
            removeItem.setRegistTime(regist.getCreateDate());
            removeItem.setTotalCount(regist.getTotalCost());
            if(regist.isHaveDeliver() == true){
                removeItem.setStatus("是");
            }
            else{
                removeItem.setStatus("否");
            }
            removeItem.setRegistDept(departmentMapper.selectById(regist.getDepartmentId()).getDeptName());
            removeItem.setRegistLEvel(registLevelMapper.selectById(regist.getRegistLevel()).getRegistLevel());
            if(regist.getDoctorId()!=null){
                removeItem.setRegistDoctor(
                        userMapper.selectById(doctorMapper.selectById(regist.getDoctorId()).getOutpatientDoctorId()
                        ).getName()
                );
            }
            list.add(removeItem);
        }
        Patient patient = patientMapper.selectById(feeRec.getPatientId());
        ReomveQSend reomveQSend = new ReomveQSend();
        PageList pageList = new PageList(list,feeRec.getCurPage(),feeRec.getPageSize());
        reomveQSend.setData(pageList);
        reomveQSend.setPatient(patientList.get(0));
        return Result.success(reomveQSend);
    }

    @Override
    public Result removeRegist(RemoveRec removeRec){
        registMapper.deleteBatchIds(removeRec.getRegistId());
        return Result.success();
    }
}
